package com.teamelite.exmgmt;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class cListUser extends ListActivity{
	private ArrayList<arrList> lstPengguna;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		DBHelper db = new DBHelper(this);
		lstPengguna = db.getPengguna("");
		setListAdapter(new myAdapter(this, lstPengguna,"Pengguna"));
		getListView().setBackgroundColor(Color.BLACK);
		registerForContextMenu(getListView());
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DBHelper db = new DBHelper(this);
		lstPengguna = db.getPengguna("");
		setListAdapter(new myAdapter(this, lstPengguna,"Pengguna"));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.clistuser_menu, menu);
        return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intents;
		switch (item.getItemId()){
		case R.id.mAdd:
			intents = new Intent(this,cAddNewUser.class);
        	this.startActivity(intents);
			return true;
		case R.id.mAbout:
			intents = new Intent(this,About.class);
        	startActivity(intents);
			return true;
        case R.id.mExit:
        	Intent intent = new Intent(Intent.ACTION_MAIN);
        	intent.addCategory(Intent.CATEGORY_HOME);
        	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        	startActivity(intent);
        	finish();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		Intent intent = new Intent(cListUser.this,cMainMenu.class);
		intent.putExtra("id_pengguna",lstPengguna.get(position).id);
		startActivity(intent);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	    getMenuInflater().inflate(R.menu.activitylist_menu, menu);
	}
	
	public AlertDialog myAlertDialog;
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
	            .getMenuInfo();
		switch (item.getItemId()) {
	    	case R.id.mRemove:
	    		
	    		if( myAlertDialog != null && myAlertDialog.isShowing() ) return true;
	            AlertDialog.Builder builder = new AlertDialog.Builder(cListUser.this);
	            builder.setTitle("Are You Sure To Remove This User?");
	            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do Nothing
					}
				});
	            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	            	@Override
	                public void onClick(DialogInterface dialog, int arg1) {
	                  	DBHelper db = new DBHelper(cListUser.this);
	                  	db.delUser(lstPengguna.get(info.position).id.toString());
	                  	lstPengguna = db.getPengguna("");
	            		setListAdapter(new myAdapter(cListUser.this, lstPengguna, "Pengguna"));
	            	}
	            });
	            builder.setCancelable(false);
	            myAlertDialog = builder.create();
	            myAlertDialog.show();
	    	return true;
	    	case R.id.mCancel:
	    	
	    	return true;
	    	default:
	    		
	    	return true;
	    }
	}
}